/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.geometry;

import org.openaecfm.model.geometry.BSplineCurve;
import org.openaecfm.model.geometry.BSplineCurveForm;
import org.openaecfm.model.geometry.CartesianPoint;
import org.openaecfm.model.measure.Logical;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.IntegerParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class BSplineCurveIO implements
		IEntityInstanceUtilInput<BSplineCurve>,
		IEntityInstanceUtilOutput<BSplineCurve> {
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void exportSPF(BSplineCurve pojo, EntityInstance ei,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Degree
		IntegerParameter degree = new IntegerParameter(pojo.getDegree());
		sei.addParameter(degree);

		// Set ControlPointsList
		ListParameter controllist = new ListParameter();
		for (CartesianPoint point : pojo.getControlPointsList()) {
			EntityInstanceNameParameter n = new EntityInstanceNameParameter(
					appDataManager.getName(point));
			controllist.add(n);
		}
		sei.addParameter(controllist);

		// Set CurveForm
		EnumerationParameter curveform = new EnumerationParameter(
				pojo.getCurveForm());
		sei.addParameter(curveform);

		// Set ClosedCurve
		EnumerationParameter close = new EnumerationParameter(
				pojo.getClosedCurve());
		sei.addParameter(close);

		// Set SelfIntersect
		EnumerationParameter self = new EnumerationParameter(
				pojo.getSelfIntersect());
		sei.addParameter(self);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveNotReferenceParameter(EntityInstance ei,
			BSplineCurve pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Degree
		IntegerParameter degree = (IntegerParameter) sei.getParameter(0);
		pojo.setDegree(degree.toInteger());

		// Set CurveForm
		EnumerationParameter curveform = (EnumerationParameter) sei
				.getParameter(2);
		pojo.setCurveForm(BSplineCurveForm.valueOf(curveform.toEnumString()));

		// Set ClosedCurve
		EnumerationParameter close = (EnumerationParameter) sei.getParameter(3);
		pojo.setClosedCurve(Logical.valueOf(close.toEnumString()));

		// Set SelfIntersect
		EnumerationParameter self = (EnumerationParameter) sei.getParameter(4);
		pojo.setSelfIntersect(Logical.valueOf(self.toEnumString()));
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, BSplineCurve pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set ControlPointsList
		ListParameter controllist = (ListParameter) sei.getParameter(1);
		for (Parameter p : controllist) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) p;
			pojo.getControlPointsList().add(
					(CartesianPoint) appDataManager.getObject(n
							.toInstanceName()));
		}
	}
}
